import {
  BrandList,
  removeBrand,
  addUser,
  editUser,
  clientDetail,
  status,
  bannerSort
} from '../api/banner';
import { message } from 'antd/lib/index';
import checkStatus from '@/utils/status';

export default {
  namespace: 'Banner',

  state: {
    data: {
      data: [],
      pagination: {},
    },
  },
  effects: {
    //请求列表
    * fetch({ payload }, { call, put }) {
      const response = yield call(BrandList, payload);
      yield checkStatus(response);
      yield put({
        type: 'get',
        payload: response.msg,
      });
    },
    //增加
    * add({ payload, callback }, { call, put }) {
      const response = yield call(addUser, payload);
      yield checkStatus(response);

      yield put({
        type: 'save',
        payload: response,
      });
      if (callback) callback();
    },

      * remove({ payload, callback }, { call, put }) {
      const response = yield call(removeBrand, payload);
      yield checkStatus(response);

      if (callback) callback(response);
    },
    * bannerSort({ payload, callback }, { call, put }) {
      const response = yield call(bannerSort, payload);
      yield checkStatus(response);

      if (callback) callback(response);
    },

    * update({ payload, callback }, { call, put }) {
      const response = yield call(editUser, payload);
      yield checkStatus(response);

      yield put({
        type: 'edit',
        payload: response,
      });
      if (callback) callback();
    },
    * status({ payload, callback }, { call, put }) {
      const response = yield call(status, payload);
      yield checkStatus(response);
      if (callback) callback(response);
    },
    * getDesc({ payload, callback }, { call, put }) {
      const response = yield call(clientDetail, payload);
      yield checkStatus(response);

      yield put({
        type: 'getdesc',
        payload: response.msg,
      });
      if (callback) callback();
    },


  },

  reducers: {
    get(state, action) {
      state.pagination = {
        pageSize: action.payload.pageSize,
        current: action.payload.pageNo,
        total: action.payload.total,
      };
      return {
        ...state,
        data: action.payload,
      };
    },
    save(state, action) {
      if (action.payload.code === 200) {
        message.success('添加成功');
        return {
          ...state,
          data: action.payload.msg,
        };
      } else {
        message.error(action.payload.errMsg);
        return {
          ...state,
          data: [],
        };
      }

    },

    edit(state, action) {
      if (action.payload.code === 200) {
        message.success('更新成功');
      } else {
        message.error(action.payload.errMsg);
      }
      return {
        ...state,
        data: action.payload,
      };
    },

    getdesc(state, action) {
      return {
        ...state,
        Desc: action.payload,
      };
    },


  },
};
